package com.easylinkin.linkappapi.openapi.dto;

import lombok.Data;

import java.util.concurrent.Delayed;
import java.util.concurrent.TimeUnit;

/**
 * 延迟主题
 */
@Data
public class DelayItem implements Delayed {
    /**
     * 触发时间
     */
    private long time;

    private String name;

    public DelayItem(String name, long time, TimeUnit unit) {
        this.name = name;
        this.time = System.currentTimeMillis() + (time > 0 ? unit.toMillis(time) : 0);
    }

    @Override
    public long getDelay(TimeUnit unit) {
        return time - System.currentTimeMillis();
    }

    @Override
    public int compareTo(Delayed o) {
        DelayItem delayItem = (DelayItem) o;
        long diff = this.time - delayItem.time;
        // 改成>=会造成问题
        if (diff <= 0) {
            return -1;
        } else {
            return 1;
        }
    }

    @Override
    public String toString() {
        return "DelayItem{" +
                "time=" + time +
                ", name='" + name + '\'' +
                '}';
    }
}
